package com.example.tabbar.fragment

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.example.tabbar.databinding.FragmentAppMsgBinding
import com.example.tabbar.databinding.FragmentSetFileBinding
import com.example.tabbar.databinding.FragmentSetParameterBinding


class AppMsgFragment : Fragment() {
    private var binding: FragmentAppMsgBinding? = null
    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var editor: SharedPreferences.Editor
    private var appVersion: TextView? = null
    private var graph: TextView? = null
    private var meaNumber: TextView? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentAppMsgBinding.inflate(inflater, container, false)
        return binding!!.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()

        // 初始化 SharedPreferences
        sharedPreferences = requireActivity().getSharedPreferences("AppMessagePrefs", Context.MODE_PRIVATE)
        editor = sharedPreferences.edit()

//        binding?.appMsgSave?.setOnClickListener {
//            saveData()
//        }
//        binding?.appMsgLoad?.setOnClickListener {
//            loadData()
//        }
        loadData()
    }

    private fun initView() {
        appVersion = binding?.appVersion
        graph = binding?.photo
        meaNumber = binding?.meaNumber
    }

    // 保存数据到 SharedPreferences
    private fun saveData() {
        editor.putString("app_name", "ZR-16XX")
        editor.putString("app_version", "v1.0.0")
        editor.putString("graph_number", "1.2.0")
        editor.apply() // 或者使用 commit()
        Toast.makeText(requireContext(), "数据已保存", Toast.LENGTH_SHORT).show()
    }
    // 从 SharedPreferences 读取数据
    private fun loadData() {
        val appName = sharedPreferences.getString("app_name", "")
        val version = sharedPreferences.getString("app_version", "")
        val graphNumber = sharedPreferences.getString("graph_number", "")
        if (appName == null) {
            Log.d("AppMsgFragment", "数据都是空的")
        } else {
            appVersion?.text = version
            graph?.text = graphNumber
            meaNumber?.text = appName
        }
    }

    // 清除所有数据
    private fun clearData() {
        editor.clear()
        editor.apply()
    }
    companion object {

        @JvmStatic
        fun newInstance() =
            AppMsgFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}